<?php
class App_Page_Service_DbStructure extends Main_Service_DbStructureAbstract
{
	/**
	 * @var App_Page_Service_DbStructure
	 */
	private static $_instance;

	/**
	 * @return App_Page_Service_DbStructure
	 */
	public static function getInstance() 
	{
		if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
	}
	
	/**
	 * @param string $driver
	 * @param string $driverType
	 * @return string
	 */
	public function getInstallSql($driver, $driverType = null)
	{
		// --- mysql --- {{
		if ($driver == 'mysql') 
		{
			// --- mysql myisam --- {{
			if ($driverType = 'myisam') 
			{
				$sql = <<<SQL
drop table if exists layout;
create table layout
(
   id                   tinyint unsigned not null auto_increment,
   name                 varchar(255) not null,
   primary key (id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

drop table if exists layout_i18n;
create table layout_i18n
(
   id                   tinyint unsigned not null auto_increment,
   layout_id            tinyint unsigned not null,
   lang_id              tinyint unsigned not null,
   title                varchar(255),
   primary key (id)
);

drop table if exists page;
create table page
(
   id                   int unsigned not null auto_increment,
   lft                  int unsigned not null,
   rgt                  int unsigned not null,
   parent_id            int unsigned,
   level                int unsigned not null,
   primary key (id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

drop table if exists page_object;
create table page_object
(
   page_id              int unsigned not null,
   name                 varchar(255) not null,
   layout_id            tinyint unsigned not null,
   f_map                tinyint(1) not null,
   f_active             tinyint(1) not null,
   primary key (page_id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

drop table if exists page_i18n;
create table page_i18n
(
   id                   int unsigned not null auto_increment,
   page_id              int unsigned not null,
   meta_title           varchar(255),
   lang_id              tinyint unsigned not null,
   meta_description     varchar(255),
   meta_keywords        varchar(255),
   primary key (id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;;

drop table if exists page_block;
create table page_block
(
   id                   int unsigned not null auto_increment,
   name                 varchar(255) not null,
   page_id              int unsigned not null,
   f_active             tinyint(1) not null,
   lang_id              tinyint unsigned,
   plugin_id            smallint unsigned,
   content              text,
   description          varchar(1000),
   primary key (id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
				
SQL;
			}
			// --- mysql myisam --- }}
		}
		// --- mysql --- }}
		
		return $sql;		
	}
	
	/**
	 * @return void
	 */
	public function setDefaultData()
	{
		$langId = Zend_Registry::getInstance()->langId;	
		
		$this->db->insert('layout', array("name"=>"'" . Main_Const::BASIC_LAYOUT. "'"));
		$layoutId = $this->db->lastInsertId('layout');
		$this->db->insert('layout_i18n', array(
			"layout_id" => $layoutId,
		 	"lang_id" => $langId,
		 	"title"	  => "'Основной'"
		));
		
		$pageData = array(
		  	'parent_id' => 'null',
			'lft' => 1,
			'rgt' => 2,
			'level' => 1,
		);
		$this->db->insert('page', $pageData);
		$pageId = $this->db->lastInsertId('page');
		
		$pageData = array(
			'page_id' => $pageId,
			'name' => "''",
		  	'layout_id' => $layoutId,
			'f_map' => 0,
			'f_active' => 0,
		);
		$this->db->insert('page_object', $pageData);
		
		$pageData = array(
			'page_id' => $pageId,
			'meta_title' => "''",
		  	'lang_id' => $langId,
			'meta_description' => "''",
			'meta_keywords' => "''",
		);
		$this->db->insert('page_i18n', $pageData);
		
		
	}
	
	
	
	
	
}
?>